namespace CSharpPro._1103;

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class UserService : IUserService
{
    private List<User> _users = new List<User>();

    public bool Login(string username, string password)
    {
        foreach (var user in _users)
        {
            if (user.Username == username && user.Password == password)
            {
                return true;
            }
        }
        return false;
    }

    public bool Register(string username, string password)
    {
        foreach (var user in _users)
        {
            if (user.Username == username)
            {
                Console.WriteLine("用户名已存在！");
                return false;
            }
        }
        
        User newUser = new User(username, password);
        _users.Add(newUser);
        return true;
    }

    public List<User> GetAllUsers()
    {
        return new List<User>(_users); 
    }

    public User FindUserByUsername(string username)
    {
        foreach (var user in _users)
        {
            if (user.Username == username)
            {
                return user;
            }
        }
        return null;
    }

    public bool DeleteUser(string username)
    {
        for (int i = 0; i < _users.Count; i++)
        {
            if (_users[i].Username == username)
            {
                _users.RemoveAt(i);
                return true;
            }
        }
        return false;
    }

    public void Run()
    {
        bool isRunning = true;
        while (isRunning)
        {
            ShowMainMenu();
            string choice = Console.ReadLine()?.Trim() ?? "";

            switch (choice)
            {
                case "1":
                    PerformLogin();
                    break;
                case "2":
                    PerformRegistration();
                    break;
                case "3":
                    ShowAllUsers();
                    break;
                case "4":
                    DeleteUserProcess();
                    break;
                case "5":
                    isRunning = false;
                    Console.WriteLine("系统已退出，再见！");
                    break;
                default:
                    Console.WriteLine("输入无效，请输入1-5之间的数字");
                    break;
            }
        }
    }

    private void ShowMainMenu()
    {
        Console.WriteLine("\n===== 用户中心 =====");
        Console.WriteLine("1. 登录");
        Console.WriteLine("2. 注册");
        Console.WriteLine("3. 显示所有用户");
        Console.WriteLine("4. 删除用户");
        Console.WriteLine("5. 退出");
        Console.Write("请选择操作: ");
    }

    private void PerformLogin()
    {
        Console.WriteLine("\n===== 账号登录 =====");

        string username;
        do
        {
            Console.Write("请输入用户名: ");
            username = Console.ReadLine()?.Trim() ?? "";
            if (string.IsNullOrEmpty(username))
                Console.WriteLine("用户名不能为空！");
        } while (string.IsNullOrEmpty(username));

        string password;
        do
        {
            Console.Write("请输入密码: ");
            password = Console.ReadLine()?.Trim() ?? "";
            if (string.IsNullOrEmpty(password))
                Console.WriteLine("密码不能为空！");
        } while (string.IsNullOrEmpty(password));

        if (Login(username, password))
        {
            Console.WriteLine($"\n登录成功！欢迎您，{username}");
            Console.WriteLine("已进入系统首页");
        }
        else
        {
            Console.WriteLine("\n登录失败！用户名或密码错误");
        }
    }

    private void PerformRegistration()
    {
        Console.WriteLine("\n===== 账号注册 =====");

        string username = GetValidUsername();
        string password = GetValidPassword();
        VerifyPassword(password);

        if (Register(username, password))
        {
            Console.WriteLine("\n注册成功！可返回主菜单进行登录");
        }
    }

    private void ShowAllUsers()
    {
        Console.WriteLine("\n===== 所有注册用户 =====");
        var users = GetAllUsers();
        
        if (users.Count == 0)
        {
            Console.WriteLine("暂无注册用户");
            return;
        }
        
        int count = 1;
        foreach (var user in users)
        {
            Console.WriteLine($"{count}. {user}");
            count++;
        }
    }

    private void DeleteUserProcess()
    {
        Console.WriteLine("\n===== 删除用户 =====");
        Console.Write("请输入要删除的用户名: ");
        string username = Console.ReadLine()?.Trim() ?? "";

        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不能为空！");
            return;
        }

        if (DeleteUser(username))
        {
            Console.WriteLine($"用户 {username} 删除成功！");
        }
        else
        {
            Console.WriteLine($"用户 {username} 不存在！");
        }
    }

    private string GetValidUsername()
    {
        while (true)
        {
            Console.Write("请设置用户名: ");
            string username = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不能为空！");
                continue;
            }

            if (username.Length < 3 || username.Length > 15)
            {
                Console.WriteLine("用户名长度必须为3-15个字符");
                continue;
            }

            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字和下划线");
                continue;
            }
            
            if (FindUserByUsername(username) != null)
            {
                Console.WriteLine("用户名已存在，请重新设置！");
                continue;
            }

            return username;
        }
    }

    private string GetValidPassword()
    {
        while (true)
        {
            Console.Write("请设置密码: ");
            string password = Console.ReadLine()?.Trim() ?? "";

            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不能为空！");
                continue;
            }

            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度必须为6-20个字符");
                continue;
            }

            bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
            bool hasLower = Regex.IsMatch(password, @"[a-z]");
            bool hasNumber = Regex.IsMatch(password, @"[0-9]");

            if (!hasUpper || !hasLower || !hasNumber)
            {
                Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字");
                continue;
            }

            return password;
        }
    }

    private void VerifyPassword(string password)
    {
        while (true)
        {
            Console.Write("请确认密码: ");
            string confirm = Console.ReadLine()?.Trim() ?? "";
            if (confirm == password)
                break;

            Console.WriteLine("两次输入的密码不一致，请重新输入");
        }
    }
}